package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品规格管理")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {

    @Autowired
    private ProductSpecService productSpecService;

    @Operation(summary = "分页带条件的查询")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec){
        List<ProductSpec> productSpecList= productSpecService.getPageList(productSpec);
        return getDataTable(productSpecList);
    }

    @Operation(summary = "添加商品规格")
    @PostMapping
    public AjaxResult save(@RequestBody ProductSpec productSpec){
        boolean save = productSpecService.save(productSpec);
        return toAjax(save);
    }

    @Operation(summary = "根据id查询商品的规格")
    @GetMapping("/{id}")
    public AjaxResult getProductSpecById(@PathVariable Long id){
        ProductSpec productSpec = productSpecService.getProductSpecById(id);
        return  AjaxResult.success(productSpec);
    }

    @Operation(summary ="根据id修改商品规格")
    @PutMapping
    public AjaxResult updategetProductSpecById(@RequestBody ProductSpec productSpec){
       boolean update= productSpecService.updateById(productSpec);
       return toAjax(update);
    }

    @Operation(summary = "删除和批量删除")
    @DeleteMapping("/{ids}")
    public AjaxResult removerById(@PathVariable List<Long> ids){
        boolean b = productSpecService.removeBatchByIds(ids);
        return toAjax(b);
    }

    @Operation(summary = "根据分类id查询商品规格")
    @GetMapping("/productSpecList/{categoryId}")
    public AjaxResult getProductSpecListByIcategoryId(@PathVariable Long categoryId){

        LambdaQueryWrapper<ProductSpec> productSpecLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productSpecLambdaQueryWrapper.eq(ProductSpec::getCategoryId,categoryId);
        List<ProductSpec> list = productSpecService.list(productSpecLambdaQueryWrapper);
        return success(list);
    }

}
